import {
    findAllClass
} from '@/services/class'

export default {
    namespace: 'class',
    state: {
        shcoolClass:[]
    },
    effects: {
        *findAllClass (_, { call, put }) {
            console.log()
            const result = yield call(findAllClass)
            yield put({
                type:'beforeChangeState',
                result,
                field: 'shcoolClass'
            })
            return result
        },

        *beforeChangeState ({ result, field }, { put }) {
            if (result.status === 0) {
                if (result.data && field) {
                    yield put({
                        type: 'changeState',
                        result,
                        field
                    })
                }
                if (result.token) {
                    yield put({
                        type: 'token/changeState',
                        token: result.token
                    })
                }
            }
        }
    },
    reducers: {
        changeState(state, { result, field }) {
            const nextState = JSON.parse(JSON.stringify({ data: state })).data
            nextState[field] = result.data
            return nextState
        }
    }
}